#!/bin/bash
#
# https://github.com/Aniverse/inexistence
# Author: Aniverse
# --------------------------------------------------------------------------------
VER=0.1.3
DATE=2018.05.24
# --------------------------------------------------------------------------------
black=$(tput setaf 0); red=$(tput setaf 1); green=$(tput setaf 2); yellow=$(tput setaf 3);
blue=$(tput setaf 4); magenta=$(tput setaf 5); cyan=$(tput setaf 6); white=$(tput setaf 7);
on_red=$(tput setab 1); on_green=$(tput setab 2); on_yellow=$(tput setab 3); on_blue=$(tput setab 4);
on_magenta=$(tput setab 5); on_cyan=$(tput setab 6); on_white=$(tput setab 7); bold=$(tput bold);
dim=$(tput dim); underline=$(tput smul); reset_underline=$(tput rmul); standout=$(tput smso);
reset_standout=$(tput rmso); normal=$(tput sgr0); alert=${white}${on_red}; title=${standout};
baihuangse=${white}${on_yellow}; bailanse=${white}${on_blue}; bailvse=${white}${on_green};
baiqingse=${white}${on_cyan}; baihongse=${white}${on_red}; baizise=${white}${on_magenta};
heibaise=${black}${on_white}; jiacu=${normal}${bold}
shanshuo=$(tput blink); wuguangbiao=$(tput civis); guangbiao=$(tput cnorm)
CW="${bold}${baihongse} ERROR ${jiacu}";ZY="${baihongse}${bold} ATTENTION ${jiacu}";JG="${baihongse}${bold} WARNING ${jiacu}"
# --------------------------------------------------------------------------------
# generate random password
function genpasswd() { local genln=$1 ; [ -z "$genln" ] && genln=12 ; tr -dc A-Za-z0-9 < /dev/urandom | head -c ${genln} | xargs ; }
# --------------------------------------------------------------------------------
# get options
OPTS=$(getopt -n "$0" -o p: --long "de,qb,rt,tr,fg,fl,password:" -- "$@")
eval set -- "$OPTS"

while true; do
  case "$1" in
    -p | --password ) ANPASS="$2"       ; shift ; shift ;;
	-r              ) RANDOM=1          ; shift         ;;
         --qb       ) ChangeQB=1        ; shift ; shift ;;
         --de       ) ChangeDE=1        ; shift ; shift ;;
         --rt       ) ChangeRT=1        ; shift ; shift ;;
         --tr       ) ChangeTR=1        ; shift ; shift ;;
         --fg       ) ChangeFG=1        ; shift ; shift ;;
         --fl       ) ChangeFL=1        ; shift ; shift ;;
    -- ) shift; break ;;
     * ) break ;;
  esac
done
[[ $RANDOM == 1 ]] && ANPASS=$(genpasswd)
# --------------------------------------------------------------------------------
local_packages=/etc/inexistence/00.Installation
inexistence=`cat /etc/inexistence/01.Log/lock/inexistence.lock | grep INEXISTENCEinstalled | cut -c22-`
INEXISTENCEVER=`cat /etc/inexistence/01.Log/lock/inexistence.lock | grep INEXISTENCEVER | cut -c16-`
INEXISTENCEDATE=`cat /etc/inexistence/01.Log/lock/inexistence.lock | grep INEXISTENCEDATE | cut -c17-`
tweaks=`cat /etc/inexistence/01.Log/lock/inexistence.lock | grep USETWEAKS | cut -c11-`
ANUSER=` ls /etc/inexistence/01.Log/lock | grep username | head -n1 | awk -F '.' '{print $2}' `
# --------------------------------------------------------------------------------

# 尝试做成没安装的显示红色，安装了的显示绿色

# 00. Main menu
function _main_menu() {

  echo -e "${green}${bold}(01) ${jiacu}Change ALL the passwords${normal}"
  echo -e "${green}${bold}(02) ${jiacu}Change password for SSH /FTP"
  echo -e "${green}${bold}(02) ${jiacu}Change password for Transmission WebUI${normal}"
  echo -e "${green}${bold}(03) ${jiacu}Change password for qBittorrent WebUI${normal}"
  echo -e "${green}${bold}(04) ${jiacu}Change password for Deluge WebUI${normal}"
  echo -e "${green}${bold}(05) ${jiacu}Change password for ruTorrent & h5ai${normal}"
  echo -e "${green}${bold}(06) ${jiacu}Change password for Flexget WebUI${normal}"
  echo -e "${green}${bold}(07) ${jiacu}Reset Flood authorization${normal}"
  echo -e "${green}${bold}(08) ${jiacu}Enable root login"
  echo -e "${green}${bold}(99) ${jiacu}Exit"
  echo -e "${normal}"

  echo -e "${normal}ZY${normal} To reset password the script will force restart the clients, fast resume data may be lost!"
}





# 00. Response
function _read_response() {
echo -e "\n${yellow}${bold}What would you like to do? (Press ${bailvse}Enter${normal}${bold} to exit)${normal}" ; read response

case $response in
    1 | 01) # Change ALL the passwords
            _askpassword
            _password_all ;;
    2 | 02) # Change password for SSH /FTP
            _askpassword
            _ssh_pass
            _read_response ;;
    3 | 03) # Change password for Transmission WebUI
            _askpassword
            _tr_pass
            _read_response ;;
    4 | 04) # Change password for qBittorrent WebUI
            _askpassword
            _qb_pass
            _read_response ;;
    5 | 05) # Change password for Deluge WebUI
            _askpassword
            _de_pass
            _read_response ;;
    6 | 06) # Change password for ruTorrent and h5ai
            _askpassword
            _rut_pass
            _read_response ;;
    7 | 07) # Change password for Flexget WebUI
            _askpassword
            _fg_pass
            _read_response ;;
    8 | 08) # Reset Flood authorization
            _askpassword
            _flood_reset
            _read_response ;;
    9 | 09) # Enable root login
            _enable_root_login
            _read_response ;;

        99) clear; exit 0 ;;
         *) clear; exit 0 ;;
esac ; }





# 01 Change ALL the passwords
function _password_all() {
_ssh_pass
_tr_pass
_qb_pass
_de_pass
_rut_pass
_fg_pass
_flood_reset
}



# 02 Change password for SSH /FTP
function _ssh_pass() {
{ echo -ne '$ANPASS\n$ANPASS\n' | passwd root ; } >/dev/null 2>&1
{ echo -ne '$ANPASS\n$ANPASS\n' | passwd root ; } >/dev/null 2>&1 ; }



# 03 Change password for Transmission WebUI
function _tr_pass() {
systemctl stop transmission
sed -i "s/RPCPASSWORD/${ANPASS}/g" /root/.config/transmission-daemon/settings.json
systemctl start transmission ; }



# 04 Change password for qBittorrent WebUI
function _qb_pass() {
systemctl stop qbittorrent
QBPASS=$(  echo -n "$ANPASS" | md5sum | cut -f1 -d' '  )
sed -i "s/.*Password_ha1.*/WebUI\\\Password_ha1=@ByteArray(${QBPASS})/" /root/.config/qBittorrent/qBittorrent.conf
systemctl start qbittorrent ; }



# 05 Change password for Deluge WebUI
function _de_pass() {
systemctl stop deluged
systemctl stop deluge-web
echo "${ANUSER}:${ANPASS}:10" > /root/.config/deluge/auth
sed -i "s/.*pwd_salt.*/  \"pwd_salt\": \"${DWSALT}\",/" /root/.config/deluge/web.conf
sed -i "s/.*pwd_sha1.*/  \"pwd_sha1\": \"${DWP}\",/" /root/.config/deluge/web.conf
systemctl start deluged
systemctl start deluge-web ; }



# 06 Change password for ruTorrent and h5ai
function _rut_pass() {
}



# 07 Change password for Flexget WebUI
function _fg_pass() { 
flexget web passwd $ANPASS >/tmp/flex.pass.output 2>&1
rm -f /etc/inexistence/01.Log/lock/flexget.{pass,conf}.lock
[[ `grep "not strong enough" /tmp/flex.pass.output` ]] && { export FlexPassFail=1 ; echo -e "\nFailed to set flexget webui password\n"            ; touch /etc/inexistence/01.Log/lock/flexget.pass.lock ; }
[[ `grep "schema validation" /tmp/flex.pass.output` ]] && { export FlexConfFail=1 ; echo -e "\nFailed to set flexget config and webui password\n" ; touch /etc/inexistence/01.Log/lock/flexget.conf.lock ; }
}


# 08 Reset Flood authorization
# https://github.com/jfurrow/flood/wiki/F.A.Q.#i-forgot-my-password-how-can-i-recover-it
function _flood_reset() { 
systemctl stop flood
rm -f /srv/flood/server/db/users.db
systemctl start flood ; }



# 09 Enable root login
function _enable_root_login() { sed -i '/.*PermitRootLogin.*/d' /etc/ssh/sshd_config ; echo 'PermitRootLogin yes' >> /etc/ssh/sshd_config ; /etc/init.d/ssh restart >/dev/null 2>&1 && echo -e '\nRoot login enabled\n' ; }



# 00 Ask password
function _askpassword() {

local password1 ; local password2 ; #local exitvalue=0
exec 3>&1 >/dev/tty

if [[ $ANPASS = "" ]]; then

    while [ -z $localpass ]; do

      # echo -n "${bold}Enter the password: ${blue}" ; read -e password1
        read -ep "${jiacu}Enter the password: ${blue}" password1 ; echo -n "${normal}"

        if [ -z $password1 ]; then

            localpass=$(genpasswd) ; # exitvalue=1
            echo "${jiacu}Random password sets to ${blue}$localpass${normal}"

        # At least [8] chars long
        elif [ ${#password1} -lt 8 ]; then

            echo "${bold}${red}ERROR${normal} ${bold}Password must be at least ${red}[8]${jiacu} chars long${normal}" && continue

        # At least [1] number
        elif ! echo "$password1" | grep -q '[0-9]'; then

            echo "${bold}${red}ERROR${normal} ${bold}Password must have at least ${red}[1] number${normal}" && continue

        # At least [1] letter
        elif ! echo "$password1" | grep -q '[a-zA-Z]'; then

            echo "${bold}${red}ERROR${normal} ${bold}Password must have at least ${red}[1] letter${normal}" && continue

        else

            while [[ $password2 = "" ]]; do
                read -ep "${jiacu}Enter the new password again: ${blue}" password2 ; echo -n "${normal}"
            done

            if [ $password1 != $password2 ]; then
                echo "${bold}${red}WARNING${normal} ${bold}Passwords do not match${normal}" ; unset password2
            else
                localpass=$password1
            fi

        fi

    done

    ANPASS=$localpass
    exec >&3- ; echo ; # return $exitvalue

else

    echo -e "${bold}Password sets to ${blue}$ANPASS${normal}\n"

fi ; }






_main_menu
_read_response



